package com.tro.mail.web;

import java.math.BigDecimal;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.tro.common.util.easyui.Pagination;
import com.tro.common.util.mybatis.Page;
import com.tro.common.util.string.AESTool;
import com.tro.mail.dao.entity.MailSetingTable;
import com.tro.mail.dao.entity.MailSetingTableExample;
import com.tro.mail.dao.mapper.MailSetingTableMapper;
import com.tro.mail.util.Json;

@Controller
@RequestMapping("/emailSeting")
public class EmailSetingController {

	@Autowired
	MailSetingTableMapper mailsetingM;
	
	/**
	 * 页面初始化
	 * */
	@RequestMapping("initEmailSeting")
	@ResponseBody
	public Pagination<MailSetingTable> initEmailSeting(String page ,String rows,HttpSession session){
		if (!StringUtils.isNotBlank(page)) {
			page = "1";
		}
		if (!StringUtils.isNotBlank(rows)) {
			rows = "20";
		}
		if (!StringUtils.isNumeric(page)) {
			page = "0";
		}
		if (!StringUtils.isNumeric(rows)) {
			rows = "0";
		}
		Pagination<MailSetingTable> Page = new Pagination<MailSetingTable>();
		MailSetingTableExample example=new MailSetingTableExample();
		Integer curPage = Integer.parseInt(page);
		Integer prePage = curPage - 1;
		Integer row = Integer.parseInt(rows);
		example.setPage(new Page(prePage * row, curPage * row));
		String curretUser=session.getAttribute("MAIL_USER_ID").toString();
		example.createCriteria().andSysCodeEqualTo(curretUser);
		Page.setPage(curPage);
		Page.setSize(row);
		Page.setRows(mailsetingM.selectByExample(example));
		Page.setTotal(mailsetingM.countByExample(example));
		return Page;
	} 
	
	
	/**
	 * 添加
	 * */
	@RequestMapping("addOrUpdateEmailSeting")
	@ResponseBody
	public String addOrUpdateEmailSeting(HttpSession session,String index,String ID,String emailPassword,String emailCode){
		MailSetingTable mst=new MailSetingTable();
		Json json=new Json();
		AESTool aes = new AESTool();
		if("1".equals(index)){
			mst.setDefult("0");
			mst.setEmailCode(emailCode);
			mst.setEmailPassword(aes.encrypt(emailPassword));
			mst.setSysCode(session.getAttribute("MAIL_USER_ID").toString());
			if(mailsetingM.insert(mst)>0){
				json.setMsg("添加成功");
				json.setSuccess(true);
			}else{
				json.setMsg("添加失败");
				json.setSuccess(false);
			}
		}else{
			mst=mailsetingM.selectByPrimaryKey(new BigDecimal(ID));
			mst.setDefult("0");
			mst.setEmailCode(emailCode);
			mst.setEmailPassword(aes.encrypt(emailPassword));
			mst.setSysCode(session.getAttribute("MAIL_USER_ID").toString());
			if(mailsetingM.updateByPrimaryKey(mst)>0){
				json.setMsg("修改成功");
				json.setSuccess(true);
			}else{
				json.setMsg("修改失败");
				json.setSuccess(false);
			}
		}
		return JSON.toJSONString(json);
	}
	
	
	/**
	 * 删除
	 * */
	@RequestMapping("remove")
	@ResponseBody
	public String remove(String ID){
		Json json=new Json();
		try {
			if(!StringUtils.isBlank(ID)){
				if(mailsetingM.deleteByPrimaryKey(new BigDecimal(ID))>0){
					json.setMsg("删除成功");
					json.setSuccess(true);
				}else{
					json.setMsg("删除失败");
					json.setSuccess(false);
				}
			}
		} catch (Exception e) {
			json.setMsg("操作异常");
			json.setSuccess(false);
		}
		return JSON.toJSONString(json);
	}
	
	
	/**
	 * 设置为默认发送邮箱
	 * */
	@RequestMapping("setDefult")
	@ResponseBody
	@Transactional
	public String setDefult(String ID,HttpSession session){
		Json json=new Json();
		try {
			//将个人有箱中原默认邮箱改为0
			MailSetingTableExample example=new MailSetingTableExample();
			example.createCriteria().andDefultEqualTo("1").andSysCodeEqualTo(session.getAttribute("MAIL_USER_ID").toString());
			List<MailSetingTable> setList=mailsetingM.selectByExample(example);
			for(MailSetingTable s:setList){
				s.setDefult("0");
				mailsetingM.updateByPrimaryKey(s);
			}
			//将点击设为默认
			MailSetingTable mailSet=mailsetingM.selectByPrimaryKey(new BigDecimal(ID));
			mailSet.setDefult("1");
			mailsetingM.updateByPrimaryKey(mailSet);
			json.setSuccess(true);
			json.setMsg("成功设置默认操作邮箱");
		} catch (Exception e) {
			e.printStackTrace();
			json.setSuccess(false);
			json.setMsg("数据操作异常");
		}
		return JSON.toJSONString(json);
	}
}
